﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LaserHarp
{

   public delegate void NoteSentHandler(object sender, EventArgs e);

    // LED array button used to send test messages to MIDI output and to display status of beam.

    class LEDarray : System.Collections.CollectionBase
    {
        public event NoteSentHandler SendNote;

        private Control container;

        public LEDarray(Control host)
        {
            container = host;
        }

        public Button AddNewButton()
        {
            // Create a new instance of the Button class.
            Button aButton = new Button();
            // Add the button to the collection's internal list.
            this.List.Add(aButton);
            // Add the button to the controls collection of the form 
            // referenced by the HostForm field.
            container.Controls.Add(aButton);
            // Set intial properties for the button object.
            aButton.Top = 16;
            aButton.Left = 70 + 50 * (Count-1);
            aButton.Width = 20;
            aButton.Tag = this.Count-1;
            aButton.Text = "";
            aButton.Click += new System.EventHandler(ClickHandler);
            return aButton;
        }

        public Button this[int Index]
        {
            get
            {
                return (Button)this.List[Index];
            }
        }

        public void Remove()
        {
            // Check to be sure there is a button to remove.
            if (this.Count > 0)
            {
                // Remove the last button added to the array from the host form 
                // controls collection. Note the use of the indexer in accessing the array.
                container.Controls.Remove(this[this.Count - 1]);
                this.List.RemoveAt(this.Count - 1);
            }
        }

        public void ClickHandler(Object sender, System.EventArgs e)
        {
           // MessageBox.Show("You have clicked button " +((Button)sender).Tag.ToString());
            SendNote(sender, e);
        }
    }
}
